//
//  UITableViewTool.m
//  OnionHome
//
//  Created by fengjie on 2019/9/13.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "UITableViewTool.h"

@interface UITableViewTool ()
/**dataArray*/
@property (nonatomic,strong)NSMutableArray*dataArray;

/**identifier*/
@property (nonatomic,strong)NSString*identifier;
/**dataSourceBlock*/
@property (nonatomic,copy)DataSourceBlock dataSourceBlock;
/**cellSelectBlock*/
@property (nonatomic,copy)CellSelectBlock cellSelectBlock;
/**headeBlock*/
@property (nonatomic,strong)HeaderRefreshBlock headeBlock;
/**footBlock*/
@property (nonatomic,copy)FootRefreshBlock footBlock;
/**cell*/
@property (nonatomic,strong)UITableViewCell*cell;
/**delegate*/
@property (nonatomic,weak)id  delegate;

@end

@implementation UITableViewTool

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style registerCellClassName:(NSString *)className identifier:(NSString *)identifier dataArray:(NSMutableArray *)dataArray delegate:(id)delegate dataSourceBlock:(DataSourceBlock)dataSourceBlock selectCell:(CellSelectBlock)selectCell{
    if ([super init] == self) {
        self.dataArray = dataArray;
        self.dataSourceBlock = [dataSourceBlock copy];
        self.cellSelectBlock = selectCell;
        self.delegate = delegate;
        self.cellHeight = 44;
        
        self.identifier = identifier;
        
        _listView = [[UITableView alloc] initWithFrame:frame style:style];
        _listView.delegate=self;
        _listView.dataSource=self;
        _listView.tableFooterView=UIView.new;
        _listView.rowHeight = UITableViewAutomaticDimension;
        _listView.estimatedRowHeight = 50.0f;
        _listView.separatorColor = SeparatorColor;

        //判断_delegate是什么类型
        if ([_delegate isKindOfClass:[UIView class]]) {
            [_delegate addSubview:_listView];
        }else if([_delegate isKindOfClass:[UIViewController class]]){
            [((UIViewController *)_delegate).view addSubview:_listView];
        }else {
            DLog(@"delegate is %@",NSStringFromClass(_delegate));
            assert(_delegate);
        }
        
        if (className == nil || [className isEqualToString:@""]) {
            assert("无效的cell");
        }else{
            Class class = NSClassFromString(className);
            [_listView registerClass:class forCellReuseIdentifier:identifier];
        }
        
        
    }
    return self;
}
-(void)setCellHeight:(CGFloat)cellHeight{
    _cellHeight=cellHeight;
}
-(void)reloadData:(NSMutableArray *)array{
    if (array.count > 0) {
        self.dataArray = array;
    }
    [self.listView reloadData];
}
#pragma mark ---<UITableViewDataSource,UITableViewDelegate>
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.listView.style == UITableViewStylePlain) {
        return 1;
    }else{
        return self.dataArray.count;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    [Refresh tableViewNoMoreData:self.listView];
    if (self.isNoMoreData) {
        [Refresh tableView:self.listView FootView:YES];
        //        [Refresh tableViewNoMoreData:self.listView];
    }else{
        [Refresh tableView:self.listView FootView:NO];
    }
    if (self.listView.style == UITableViewStylePlain) {
        return self.dataArray.count;
    }else{
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:self.identifier forIndexPath:indexPath];
    id  data = nil;
    if (self.listView.style == UITableViewStylePlain) {
         data = self.dataArray[indexPath.row];
    }else{
         data = self.dataArray[indexPath.section];
    }
    BlockSafeRun(self.dataSourceBlock,cell,indexPath,data);
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return UIView.new;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return UIView.new;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    id data = self.dataArray[indexPath.row];
    BlockSafeRun(self.cellSelectBlock,cell,indexPath,data);
}
#pragma mark 外部调用方法
//添加下拉刷新
-(void)addHeaderTextColor:(UIColor *)textColor RefreshBlock:(HeaderRefreshBlock)block{
    [Refresh addTableViewRefresh:self.listView Target:self refreshingAction:@selector(headerRefreshAction) haderTextColor:textColor];
    self.headeBlock = block;
}
-(void)headerRefreshAction{
    BlockSafeRun(self.headeBlock);
}
//添加上拉加载
-(void)addFootRefresh:(FootRefreshBlock)block{
    [Refresh addTableViewFootRefresh:self.listView Target:self refreshingAction:@selector(footRefreshAction)];
    self.footBlock = block;
}
//添加上拉加载
-(void)addFootTextColor:(UIColor *)textColor Refresh:(FootRefreshBlock)block{
    [Refresh addTableViewFootRefresh:self.listView Target:self refreshingAction:@selector(footRefreshAction) haderTextColor:textColor];
    self.footBlock = block;
}
/**进入刷新状态*/
-(void)beginRefreshing{
    [Refresh beginRefreshingTableView:self.listView];
}
-(void)footRefreshAction{
    BlockSafeRun(self.footBlock);
}
//结束刷新
-(void)endRefreshing{
    [Refresh endRefreshingTableView:self.listView];
}


@end
